package com.sxt.sys.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sxt.sys.common.AppFileUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ZipUtil;

/**
 * 文件上传
 * @author LJH
 *
 */
@RestController
@RequestMapping("file")
public class FileController {


	/**
	 * 文件上传
	 */
	@RequestMapping("uploadFile")
	public Map<String,Object> uploadFile(MultipartFile mf) {
		//1,得到文件名
		String oldName = mf.getOriginalFilename();
		//2,根据文件名生成新的文件名
		String newName=AppFileUtils.createNewFileName(oldName);
		//3,得到当前日期的字符串
		String dirName=DateUtil.format(new Date(), "yyyy-MM-dd");
		//4,构造文件夹
		File dirFile=new File(AppFileUtils.UPLOAD_PATH,dirName);
		//5,判断当前文件夹是否存在
		if(!dirFile.exists()) {
			dirFile.mkdirs();//创建文件夹
		}
		//6,构造文件对象
		File file=new File(dirFile, newName+"_temp");
		//7,把mf里面的图片信息写入file
		try {
			mf.transferTo(file);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("path", dirName+"/"+newName+"_temp");
		return map;
	}
	/**
	 * 视频上传
	 */
	@RequestMapping("uploadVideo")
	public Map<String,Object> uploadVideo(MultipartFile mf) {
		//1,得到文件名
		String oldName = mf.getOriginalFilename();
		//2,根据文件名生成新的文件名
		String newName=AppFileUtils.createNewFileName(oldName);
		//3,得到当前日期的字符串
		String dirName=DateUtil.format(new Date(), "yyyy-MM-dd");
		//4,构造文件夹
		File dirFile=new File(AppFileUtils.UPLOAD_PATH,dirName);
		//5,判断当前文件夹是否存在
		if(!dirFile.exists()) {
			dirFile.mkdirs();//创建文件夹
		}
		//6,构造文件对象
		File file=new File(dirFile, newName+"_temp");
		//7,把mf里面的视频信息写入file
		try {
			mf.transferTo(file);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("path", dirName+"/"+newName+"_temp");
		return map;
	}
	/**
	 * 模板zip格式  上传  和  解压
	 */
	@RequestMapping("uploadModel")
	public Map<String,Object> uploadModel(MultipartFile mf) {
		//1,得到文件名
		String oldName = mf.getOriginalFilename();
		//2,根据文件名生成新的文件名
		String newName=AppFileUtils.createNewFileName(oldName);
		//3,得到当前日期的字符串
		String dirName=DateUtil.format(new Date(), "yyyy-MM-dd");
		//4,构造文件夹
		File dirFile=new File(AppFileUtils.UPLOAD_PATH,dirName);
		//5,判断当前文件夹是否存在
		if(!dirFile.exists()) {
			dirFile.mkdirs();//创建文件夹
		}
		//6,构造文件对象
		File file=new File(dirFile, newName);
		//7,把mf里面的视频信息写入file
		try {
			mf.transferTo(file);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		//解压压缩包
		 ZipUtil.unzip(file);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("path", dirName+"/"+newName);
		return map;
	}
	
	
	/**
	 * 图片下载
	 */
	@RequestMapping("showImageByPath")
	public ResponseEntity<Object> showImageByPath(String path){
		return AppFileUtils.createResponseEntity(path);
	}
	
	
	/**
	 * zip文件下载
	 * @throws IOException 
	 */
	@RequestMapping("showMedolByPath")
	public ResponseEntity<Object> showMedolByPath(HttpServletResponse response,String path) throws IOException{
		 File file = new File(AppFileUtils.UPLOAD_PATH,path); // 要下载的文件绝对路径  
         InputStream ins;    

         ins = new BufferedInputStream(new FileInputStream(file));  
         
         byte[] buffer = new byte[ins.available()];  
         ins.read(buffer);  
         ins.close();  
         response.reset();  
         response.addHeader("Content-Disposition", "attachment;filename="+ new String(file.getName().getBytes()));  
         response.addHeader("Content-Length", "" + file.length());  
         OutputStream ous = new BufferedOutputStream(response.getOutputStream());  
         response.setContentType("application/octet-stream");  
         ous.write(buffer);  
         ous.flush();  
         ous.close();  
		return AppFileUtils.createResponseEntity(path);
	}
	
	
	
}
